package as.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import as.entity.AttendanceRecord;
import as.entity.Course;
import as.entity.Register;

public class CourseDAO extends HibernateDaoSupport {
	public void saveCourse(Course course) {

		this.getHibernateTemplate().save(course);
	}

	public Course findCourseIs(long id) {
		Course course = (Course) this.getHibernateTemplate().get(Course.class,
				id);
		return course;
	}

	public List<Course> findAllCoursesName() {
		final String hql = "from Course course group by course.name ";
		List<Course> list = new ArrayList<Course>();
		return list = this.getHibernateTemplate().find(hql);

	}

	public List<Course> findAllCourseStarTime() {
		final String hql = "from Course course group by course.startTime order by course.startTime desc";
		List<Course> list = new ArrayList<Course>();
		return list = this.getHibernateTemplate().find(hql);
	}

	public List<Course> findAllCourse() {
		final String hql = "from Course course order by course.id desc";
		List<Course> list = new ArrayList<Course>();
		return list = this.getHibernateTemplate().find(hql);
	}

	public void removeCourse(Course course) {
		this.getHibernateTemplate().delete(course);
	}

	public void updateCourse(Course course) {
		this.getHibernateTemplate().update(course);
	}

	public List<Course> list(Date startTime, Date endTime) {
		String hql = "from Course as course where"
				+ " course.startTime >= ? and  course.startTime <= ?  "
				+ " order by course.startTime desc";
		Session session = this.getSession();
		List<Course> list = (List<Course>) session.createQuery(hql).setDate(0,
				startTime).setDate(1, endTime).list();
		
		session.close();
		return list;
	}

}
